﻿using ThresholdAssistant.DataContract.Result;
using System;
using System.Threading.Tasks;

namespace ThresholdAssistant.DataContract.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class ConsumerContractResultExtensions
    {
        /// <summary>
        /// 转换回调结果
        /// </summary>
        /// <param name="source"></param>
        /// <param name="merchantNo">商家编号</param>
        /// <param name="extend">透传字段</param>
        /// <returns></returns>
        public static ConsumerContractResult ToConsumerResult(this ContractResult source, string merchantNo = default, string extend = default)
        {
            return new ConsumerContractResult()
            {
                Type = default,
                Callback = false,
                CallbackData = null,
                Extend = extend,
                MerchantNo = merchantNo,
                Success = source.Success,
                SerialNo = source.SerialNo,
                ErrorCode = source.ErrorCode,
                ServiceTime = source.ServiceTime,
                ErrorMessage = source.ErrorMessage
            };
        }

        /// <summary>
        /// 设置商家回调
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type">回调类型</param>
        /// <param name="data">回调参数</param>
        /// <param name="merchantNo">商家编号</param>
        /// <param name="extend">透传字段</param>
        /// <returns></returns>
        public static ConsumerContractResult ToCallback(this ContractResult source, Enum type = default, object data = null, string merchantNo = default, string extend = default)
        {
            return new ConsumerContractResult()
            {
                Type = type,
                Callback = true,
                Extend = extend,
                CallbackData = data,
                MerchantNo = merchantNo,
                Success = source.Success,
                SerialNo = source.SerialNo,
                ErrorCode = source.ErrorCode,
                ServiceTime = source.ServiceTime,
                ErrorMessage = source.ErrorMessage
            };
        }

        /// <summary>
        /// 设置成业务处理结果
        /// </summary>
        /// <param name="source"></param>
        /// <param name="merchantNo">商家编号</param>
        /// <param name="extend">透传字段</param>
        /// <returns></returns>
        public static Task<ConsumerContractResult> ToResultAsync(this Task<ContractResult> source, string merchantNo = default, string extend = default)
        {
            return source.ContinueWith(task => task.Result.ToConsumerResult(merchantNo, extend));
        }

        /// <summary>
        /// 转换成
        /// </summary>
        /// <param name="source">数据结果</param>
        /// <param name="merchantNo">商家编号</param>
        /// <param name="extend">透传字段</param>
        /// <param name="data">回调数据</param>
        /// <returns></returns>
        public static Task<ConsumerContractResult> ToCallbackAsync(this Task<ContractResult> source, string merchantNo = default, string extend = default, object data = default)
        {
            return source.ContinueWith(task => task.Result.ToCallback(default, data, merchantNo, extend));
        }

        /// <summary>
        /// 转换成
        /// </summary>
        /// <param name="source">数据结果</param>
        /// <param name="merchantNo">商家编号</param>
        /// <param name="type">回调编码</param>
        /// <param name="extend">透传字段</param>
        /// <param name="data">回调数据</param>
        /// <returns></returns>
        public static Task<ConsumerContractResult> ToCallbackAsync(this Task<ContractResult> source, string merchantNo = default, Enum type = default, string extend = default, object data = default)
        {
            return source.ContinueWith(task => task.Result.ToCallback(type, data, merchantNo, extend));
        }
    }
}